package cn.yzx.pethome.system.pet.service.impl;

import cn.yzx.pethome.system.menu.domain.Menu;
import cn.yzx.pethome.system.pet.domain.PetType;
import cn.yzx.pethome.system.pet.mapper.PetTypeMapper;
import cn.yzx.pethome.system.pet.service.IPetTypeService;
import cn.yzx.pethome.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yanzixuan
 * @since 2023-02-05
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> typeTree() {
        List<PetType> petTypes = petTypeMapper.findAll();

        List<PetType> typeTree = new ArrayList<>();

        for(PetType petType : petTypes){
            if(petType.getPid() == null){
                typeTree.add(petType);
            }else{
                for(PetType parent : petTypes){
                    if (petType.getPid() == parent.getId()){
                        parent.getChildren().add(petType);
                    }
                }
            }
        }
        return typeTree;
    }
}
